package com.ruoyi.report_gc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_gc.domain.bo.GjhjjtbgBsDetailBo;
import com.ruoyi.report_gc.domain.vo.GjhjjtbgBsDetailVo;
import com.ruoyi.report_gc.domain.GjhjjtbgBsDetail;
import com.ruoyi.report_gc.mapper.GjhjjtbgBsDetailMapper;
import com.ruoyi.report_gc.service.IGjhjjtbgBsDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 钢筋焊接接头试验检测报告详情Service业务层处理
 *
 * @author llb
 * @date 2024-06-24
 */
@RequiredArgsConstructor
@Service
public class GjhjjtbgBsDetailServiceImpl implements IGjhjjtbgBsDetailService {

    private final GjhjjtbgBsDetailMapper baseMapper;

    /**
     * 查询钢筋焊接接头试验检测报告详情
     */
    @Override
    public GjhjjtbgBsDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询钢筋焊接接头试验检测报告详情列表
     */
    @Override
    public TableDataInfo<GjhjjtbgBsDetailVo> queryPageList(GjhjjtbgBsDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GjhjjtbgBsDetail> lqw = buildQueryWrapper(bo);
        Page<GjhjjtbgBsDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询钢筋焊接接头试验检测报告详情列表
     */
    @Override
    public List<GjhjjtbgBsDetailVo> queryList(GjhjjtbgBsDetailBo bo) {
        LambdaQueryWrapper<GjhjjtbgBsDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GjhjjtbgBsDetail> buildQueryWrapper(GjhjjtbgBsDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GjhjjtbgBsDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, GjhjjtbgBsDetail::getExcelId, bo.getExcelId());
        lqw.eq(StringUtils.isNotBlank(bo.getShijbh()), GjhjjtbgBsDetail::getShijbh, bo.getShijbh());
        lqw.eq(StringUtils.isNotBlank(bo.getHanjzl()), GjhjjtbgBsDetail::getHanjzl, bo.getHanjzl());
        lqw.eq(StringUtils.isNotBlank(bo.getGangjph()), GjhjjtbgBsDetail::getGangjph, bo.getGangjph());
        lqw.eq(StringUtils.isNotBlank(bo.getZhij()), GjhjjtbgBsDetail::getZhij, bo.getZhij());
        lqw.eq(StringUtils.isNotBlank(bo.getJiszb()), GjhjjtbgBsDetail::getJiszb, bo.getJiszb());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjg()), GjhjjtbgBsDetail::getJiancjg, bo.getJiancjg());
        lqw.eq(StringUtils.isNotBlank(bo.getDankxs()), GjhjjtbgBsDetail::getDankxs, bo.getDankxs());
        lqw.eq(StringUtils.isNotBlank(bo.getWanqjg()), GjhjjtbgBsDetail::getWanqjg, bo.getWanqjg());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), GjhjjtbgBsDetail::getJiegpd, bo.getJiegpd());
        return lqw;
    }

    /**
     * 新增钢筋焊接接头试验检测报告详情
     */
    @Override
    public Boolean insertByBo(GjhjjtbgBsDetailBo bo) {
        GjhjjtbgBsDetail add = BeanUtil.toBean(bo, GjhjjtbgBsDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改钢筋焊接接头试验检测报告详情
     */
    @Override
    public Boolean updateByBo(GjhjjtbgBsDetailBo bo) {
        GjhjjtbgBsDetail update = BeanUtil.toBean(bo, GjhjjtbgBsDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GjhjjtbgBsDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除钢筋焊接接头试验检测报告详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
